package v3.object.command.list;

import galaxy.obj.Point;

import java.util.Map;

import text.TextUtil;
import v3.object.STcfg;
import v3.object.command.aCommand;
import v3.object.command.iCommand;
import v3.object.command.servoCommand.ComputerMessage;
import v3.object.command.servoCommand.Confirmation;
import v3.object.ship.Allert;
import v3.object.ship.Ship;
import v3.object.space.Universe;

public class Warp extends aCommand
{
	private double w;
	private int x;
	private int y;
	private Point newDestionation;
	private Point q;
	private double d;
	private double e;
	private double t;
	private double wfactor;

	public Warp()
	{
		super();
		commandName = "WARP";
	}
	
	@Override
	public void getHelp(Ship ship)
	{
		String help = commandName+" [w]"+ 
				"Format: "+commandName+" X Y WarpFacotr\n" + 
				"Allows to travel faster than light into the sector X Y at a certain kind of (speed) Warp Factor[0-10)\n " 
				+ "Time Cost " + ship.getTimeConsume((this.getClass())) + "\n" 
				+ "Energy Cost " + ship.getEnergyConsume((this.getClass())) + "";
		help = TextUtil.formatHelp(help);
		System.out.println(help);
		
	}

	@Override
	protected boolean isCommandAccetable(STcfg cfg, Universe universe, Ship ship, Map<String, iCommand> commands, String[] input)
	{
		if(ship.allert.equals(Allert.RED))
		{
			iCommand c = new ComputerMessage("Under RED ALLERT condition WARP command is not available, the ship must DISENGAGE before");
			c.execute(cfg, universe, ship, commands, input);
			return false;
		}
		return true;
	}

	@Override
	protected double calculateEnergy(STcfg cfg, Universe universe, Ship ship, Map<String, iCommand> commands, String[] input)
	{
		e = d* ship.getEnergyConsume(this.getClass())/wfactor;
		return e;
	}

	@Override
	protected double calculateTime(STcfg cfg, Universe universe, Ship ship, Map<String, iCommand> commands, String[] input)
	{
		newDestionation = new Point(x,y,0);
		d =ship.getPosition().distance(newDestionation)/10;
		wfactor=(1.-w/10.);
		t = d* ship.getTimeConsume(this.getClass())*wfactor;
		return t;
	}

	@Override
	protected void checkInput(String[] input) throws Exception
	{
		x= Integer.parseInt(input[1]);
		y= Integer.parseInt(input[2]);
		w= Double.parseDouble(input[3]);
	}

	@Override
	protected boolean realExecute(STcfg cfg, Universe universe, Ship ship, Map<String, iCommand> commands, String[] input)
	{
		ship.setPosition(newDestionation);
		try
		{
			Point nsp =universe.getSector(newDestionation).getFreeSectorPoint();
			ship.setSectorPosition(nsp);
			universe.getSector(newDestionation).setISpaceObj(nsp, ship);
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		iCommand ss = new ShortSensor();
		ss.execute(cfg, universe, ship, commands, input);
		if(universe.thereAreEnemy(newDestionation))
		{
			iCommand msg = new ComputerMessage("*** There are Enemyes in the sector: Sneak Attack! ***");
			msg.execute(cfg, universe, ship, commands, input);
		}
		return true;
	}


	@Override
	protected boolean confirmCommand(STcfg cfg, Universe universe, Ship ship, Map<String, iCommand> commands, String[] input)
	{
		iCommand conf = new Confirmation(commandName+": INFO","\n\ndistance:\t",
				format_decimal.format(d*10.)+"","\nEnergy:  \t",
				format_decimal.format(e)+"" , "\nTime:   \t",
				format_decimal.format(t)+"\n");
				return conf.execute(cfg, universe, ship, commands, input);
	}

}
